/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.ui.component.tabel.models;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.opu.db_vdumper.ui.View;
import org.opu.db_vdumper.ui.component.tabel.models.Model;
import org.opu.db_vdumper.ui.component.tabel.models.TableModel;

/**
 *
 * @author yura
 */
public class SchemaModel extends Model implements Iterable<TableModel> {
    private Set<TableModel> tables;
    private String name;

    public SchemaModel(String name) {
        this(name, new HashSet<TableModel>());
    }

    public SchemaModel(String name, Set<TableModel> columns) {
        this.name = name;
        this.tables = columns;
    }

    public void setTables(Set<TableModel> columns) {
        this.tables.clear();
        this.tables.addAll(columns);
    }

    public Set<TableModel> getTables() {
        return tables;
    }

    public boolean add(TableModel e) {
        return tables.add(e);
    }

    public int size() {
        return tables.size();
    }

    @Override
    public Iterator<TableModel> iterator() {
        return tables.iterator();
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);

        for (TableModel tbl : tables) {
            tbl.setSelected(selected);
        }
    }

    @Override
    public boolean isSelected() {
        for (TableModel tbl : tables) {
            if (tbl.isSelected()){
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean hasChildrens() {
        return !tables.isEmpty();
    }

    @Override
    public Collection<TableModel> getChildrens() {
        return tables;
    }

    @Override
    public String toString() {
        return String.valueOf(name);
    }
}
